--
-- Author: shunguo.chen
-- Date: 2017-11-27 11:42:45
--
-- MountRender 坐骑

local ClassRef = app.mvc.createRenderClass(...)

local SLOT_GAP = 12

--data = Mount
function ClassRef.create(node, isMultipleLayerNode)
	if not node then
		node = display.createMultipleLayerRenderWithCsbPath(Res.MountNode, isMultipleLayerNode)
	end
	local sprite = ClassRef.extend(node)
	sprite:init()
	return sprite
end

-- Mount
function ClassRef:init(data)
    self.super.init(self, data)
    
    local panel = self:getChildByName("Panel")
    if panel then
        self._panel = panel
    else
        self._panel = self
    end

	self:setContentSize(self._panel:getContentSize())

    self._selected = self._panel:getChildByName("selected")
    self._selected:retain()
    self._selected:removeFromParent()
    self:addChild(self._selected)
    self._selected:release()
	self:setSelected(false)

    self._panel:setSwallowTouches(false)
    display.setCascadeOpacityEnabled(self._panel)
    self:setClickEnable(true)

	display.uiAddClick(self._panel, function(sender, eventType)
        -- 清理isNew
        if self._data and self._data.isNew then
            self._data:setIsNewGet(false)
            self:updateRedPoint()
            UD.redPoint:checkStateByKey(RedPointConstant.Mount_Ride)
            UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_MOUNT_PIT)
        end

        if not self:isSelected() then
		    self:setSelected(not self:isSelected())
            executeFunc(self._onClick, self)
            self:dispatchCellEvent("click", self)
        end
	end)

    self:setCanOperation(false)
    self:setRedPoint(false)

    self.championFlag = self._panel:getChildByName("champion")
	self:refresh()
end

function ClassRef:setNewScale(len)
    self:setAnchorPoint(cc.p(0.5, 0.5))
	local size = self:getContentSize()
	local scale = len / size.height
	self:setScale(scale)
end

function ClassRef:refresh()
	if self._data then
		local data = self._data
		self:setNewTipVisibla(data.isNew)
 	    self:setIcon(data:getIcon())
		self:setIconKuang(data:getIconBg())
        self:setRiding(data:isRiding())

        local isHave = data:haveMount()
        self:setHave(isHave)
        self:setStar(isHave and data:getStar() or 0)

        self:updateCanOperation()
        self:updateRedPoint()

        if data.conf.type2icon then
            self.championFlag:loadTexture(string.format("%s%s.png",Res.commonUIPath,data.conf.type2icon))
        end
        self.championFlag:setVisible(data.conf.type2icon ~= nil)

        self:refreshUIState()
    end
    
end

-- 更新是否可以 升星 或者 提升品质
function ClassRef:updateCanOperation()
    if self._data and self:isActivePanelList() then
        self:setCanOperation(self._data:checkCanOperation())
    end
end

function ClassRef:updateRedPoint()
    local data = self._data
    local visible = false
    if data then
        if self:isMountPanelList() then
            visible = data.isNew or data:checkComposition()
        elseif self:isActivePanelList() then
            visible = data.isNew or data:checkActiveOp() or data:checkComposition()
        end
    end
    self:setRedPoint(visible)
end

function ClassRef:setHave(b)
    self._ui.mask:setVisible(not b)
    self._ui.null_bg:setVisible(not b)

    -- 未获得变灰
    -- local state = b and 0 or 1
    -- self._ui.icon:getVirtualRenderer():setState(state)

    -- 未获得设置透明度
    self._panel:setOpacity(b and 255 or 255*0.7)
end

function ClassRef:setMaskVisible(b)
	self._ui.mask:setVisible(b)
end

function ClassRef:setIconKuang(res)
	self._ui.grade:loadTexture(res)
end

function ClassRef:setIcon(res)
    self._ui.icon:setVisible(true)
	self._ui.icon:loadTexture(res)
end

function ClassRef:setRiding(b)
    self._ui.equipped:setVisible(b)
end

function ClassRef:setCanOperation(b)
    self._ui.img_ContinueFlag:setVisible(b)
    local action = self:getTimeline()
    if action then
        local isPlaying = action:isPlaying()
        if b and not isPlaying then
            action:gotoFrameAndPlay(0, true)
        elseif not b and isPlaying then
            action:gotoFrameAndPause(0)
        end
    end
end

function ClassRef:setRedPoint(b)
    self._ui.dotNew:setVisible(b)
end

function ClassRef:setStar(star)
    self._ui.Panel_star:setVisible(star > 0)
    if star > 0 then
        -- 星星从中间开始（暂时不用）
        -- local midX = self._ui.Panel_star:getContentSize().width/2
        -- local startX = midX - (star - 1) / 2 * SLOT_GAP

        -- for i=1,5 do
        --     local node = self._ui.Panel_star:getChildByName("s"..i)
        --     node:setVisible(i<=star)
        --     if i <= star then
        --         node:setPositionX(startX + (i-1) * SLOT_GAP)
        --     end
        -- end

        -- 星星从左边开始
        for i=1,5 do
            local node = self._ui.Panel_star:getChildByName("s"..i)
            node:setVisible(i<=star)
        end
    end
end

function ClassRef:hideStarPanel()
    self._ui.Panel_star:setVisible(false)
end

function ClassRef:setNewTipVisibla(b)
	-- self._ui.Icon_tips:setVisible(b)
	-- if b then
	-- 	display.playTimeline(self._ui.Icon_tips, nil, true)
	-- else
	-- 	display.pauseTimeline(self._ui.Icon_tips)
	-- end
end


function ClassRef:isSelected()
    return self._selected:isVisible()
end
function ClassRef:setSelected(b)
    self._selected:setVisible(b)
    self:setUISelectedState(b)
end

function ClassRef:setClickEnable(b)
	self._panel:setTouchEnabled(b)
end

function ClassRef:setClickHandler(cb)
	self._onClick = cb
end
function ClassRef:setBtnClick(cb)
    self._onClick = cb
end



function ClassRef:setCellModel(cellModel)
	self._cellModel = cellModel
end

function ClassRef:getCellModel()
	return self._cellModel
end

--在 listview 里面保存 UI状态
function ClassRef:setUISelectedState(b)
	if self._cellModel then
		self._cellModel:setUIState("selected", b)
	end
end
-- 在 listview 分发点击事件
function ClassRef:dispatchCellEvent(eventName, params)
	if self._cellModel then
		self._cellModel:dispatchEvent(eventName, params)
	end
end

function ClassRef:refreshUIState()
	if self._cellModel then
		self:setSelected(self._cellModel:getUIState("selected") == true)
	end
end

function ClassRef:isActivePanelList()
    if self._cellModel then
        local cellShareData = self._cellModel:getShareData()
        if cellShareData then
            return cellShareData.listviewType == Enums.MountListViewType.ActivePanelType
        end
    end
    return false
end

function ClassRef:isMountPanelList()
    if self._cellModel then
        local cellShareData = self._cellModel:getShareData()
        if cellShareData then
            return cellShareData.listviewType == Enums.MountListViewType.MountPanelType
        end
    end
    return false
end


return ClassRef